#!/usr/bin/python

import pprint
import csv

import util
import cfg


def sales_country_week_ob(data_source, year, week, result):
    print("\n----processing sales_country_week_ob......")
    print("data_source:%s" % data_source)
    print("year:%s" % (year))
    print("week:%s" % (week))
    print("result:%s" % result)

    #0.  prepare input & output struct 
    # for [week,bond,sales_country] data
    week_rtab = cfg.init_input_tab()
    # year's acc_week result for [year,bond,sales_country] data
    acc_year_rtab =  cfg.init_input_tab()
    # the final struct for all data item, trade_lines existing in the 2015 or 2016
    all_struct_rtab = cfg.init_result_tab()

    #1.  read csv file
    csvfile = open(data_source, 'rt')
    reader = csv.reader(csvfile)
    ds_idx = cfg.ds_idx_1_2_3_2
    for line in reader:
        if reader.line_num > 1 and cfg.filter_OB(line, ds_idx):
            tue         = util.stof(util.get_arr_key_val(line, ds_idx.tue, ""))
            target      = util.stof(util.get_arr_key_val(line, ds_idx.target, ""))
            fre_rev     = util.stof(util.get_arr_key_val(line, ds_idx.fre_rev, ""))
            oth_rev     = util.stof(util.get_arr_key_val(line, ds_idx.oth_rev, ""))
            tar_rev     = util.stof(util.get_arr_key_val(line, ds_idx.tar_rev, ""))

            key = "%s,%s,%s" % (util.get_arr_key_val(line, ds_idx.week, ""),util.get_arr_key_val(line, ds_idx.bond, ""), util.get_arr_key_val(line, ds_idx.sales_country, ""))
            week_rtab["tue"][key]        = util.add_if(key, week_rtab["tue"],      tue  ,     0.0)
            week_rtab["target"][key]     = util.add_if(key, week_rtab["target"],   target  ,  0.0)
            week_rtab["fre_rev"][key]     = util.add_if(key, week_rtab["fre_rev"],   fre_rev  ,  0.0)
            week_rtab["oth_rev"][key]    = util.add_if(key, week_rtab["oth_rev"],  oth_rev  , 0.0)
            week_rtab["tar_rev"][key]    = util.add_if(key, week_rtab["tar_rev"],  tar_rev  , 0.0)

            # get the year bond sales_country acc_result
            key = "%s,%s,%s" % (util.get_arr_key_val(line, ds_idx.year, ""),util.get_arr_key_val(line, ds_idx.bond, ""), util.get_arr_key_val(line, ds_idx.sales_country, ""))
            acc_year_rtab["tue"][key]        = util.add_if(key, acc_year_rtab["tue"],      tue  ,     0.0)
            acc_year_rtab["target"][key]     = util.add_if(key, acc_year_rtab["target"],   target  ,  0.0)
            acc_year_rtab["fre_rev"][key]     = util.add_if(key, acc_year_rtab["fre_rev"],   fre_rev  ,  0.0)
            acc_year_rtab["oth_rev"][key]    = util.add_if(key, acc_year_rtab["oth_rev"],  oth_rev  , 0.0)
            acc_year_rtab["tar_rev"][key]    = util.add_if(key, acc_year_rtab["tar_rev"],  tar_rev  , 0.0)


            # get the all struct for result
            key = "%s,%s" % (util.get_arr_key_val(line, ds_idx.bond, ""), util.get_arr_key_val(line, ds_idx.sales_country, ""))
            all_struct_rtab["tue"][key]        = util.add_if(key, all_struct_rtab["tue"],      0.0  ,     0.0)
        #end if
    #end for

    csvfile.close()


    #2. gen result struct 
    last_year = int(year) - 1
    for key in all_struct_rtab["tue"] :
        week_key = "%s%02d,%s" % (year,week,key)
        all_struct_rtab["tue"][key] = util.stof(util.get_tab_key_val(week_rtab["tue"],week_key, ""))
        all_struct_rtab["target"][key] = util.stof(util.get_tab_key_val(week_rtab["target"],week_key, ""))
        all_struct_rtab["fre_rev"][key] = util.stof(util.get_tab_key_val(week_rtab["fre_rev"],week_key, ""))
        all_struct_rtab["oth_rev"][key] = util.stof(util.get_tab_key_val(week_rtab["oth_rev"],week_key, ""))
        all_struct_rtab["tar_rev"][key] = util.stof(util.get_tab_key_val(week_rtab["tar_rev"],week_key, ""))

        week_key = "%s%02d,%s" %(last_year, week, key)
        all_struct_rtab["last_tue"][key] = util.stof(util.get_tab_key_val(week_rtab["tue"],week_key, ""))
        all_struct_rtab["last_fre_rev"][key] = util.stof(util.get_tab_key_val(week_rtab["fre_rev"],week_key, ""))
        all_struct_rtab["last_oth_rev"][key] = util.stof(util.get_tab_key_val(week_rtab["oth_rev"],week_key, ""))

        acc_key = "%s,%s" % (year, key)
        all_struct_rtab["acc_tue"][key]     = util.stof(util.get_tab_key_val(acc_year_rtab["tue"], acc_key, ""))
        all_struct_rtab["acc_target"][key]  = util.stof(util.get_tab_key_val(acc_year_rtab["target"], acc_key, ""))
        all_struct_rtab["acc_fre_rev"][key] = util.stof(util.get_tab_key_val(acc_year_rtab["fre_rev"], acc_key, ""))
        all_struct_rtab["acc_oth_rev"][key] = util.stof(util.get_tab_key_val(acc_year_rtab["oth_rev"], acc_key, ""))
        all_struct_rtab["acc_tar_rev"][key] = util.stof(util.get_tab_key_val(acc_year_rtab["tar_rev"], acc_key, ""))

        acc_key = "%d,%s" % (last_year, key)
        all_struct_rtab["acc_last_tue"][key]    = util.stof(util.get_tab_key_val(acc_year_rtab["tue"], acc_key, ""))
        all_struct_rtab["acc_last_fre_rev"][key] = util.stof(util.get_tab_key_val(acc_year_rtab["fre_rev"], acc_key, ""))
        all_struct_rtab["acc_last_oth_rev"][key] = util.stof(util.get_tab_key_val(acc_year_rtab["oth_rev"], acc_key, ""))
        
    # format the result and write to result_file
    out_str = []
    for key in all_struct_rtab["tue"]:
        s = "%s,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f\n" %  (key,
                all_struct_rtab["tue"][key],
                all_struct_rtab["target"][key],
                all_struct_rtab["last_tue"][key],
                all_struct_rtab["acc_tue"][key],
                all_struct_rtab["acc_target"][key],
                all_struct_rtab["acc_last_tue"][key],
                all_struct_rtab["fre_rev"][key],
                all_struct_rtab["oth_rev"][key],
                all_struct_rtab["tar_rev"][key],
                all_struct_rtab["last_fre_rev"][key],
                all_struct_rtab["last_oth_rev"][key],
                all_struct_rtab["acc_fre_rev"][key],
                all_struct_rtab["acc_oth_rev"][key],
                all_struct_rtab["acc_tar_rev"][key],
                all_struct_rtab["acc_last_fre_rev"][key],
                all_struct_rtab["acc_last_oth_rev"][key],
            )
        out_str.append(s)
    out_str.sort(key=str.lower)

    util.write_file(result, out_str)
    print("----sales_country_week_ob  OK\n")

if __name__ == "__main__":
    data_source = "source/2024 wk10 1713.csv"
    week        = 10
    year        = 2024

    result="result/result_sales_country_week_ob_%s%02d.csv" %(year,week)
    sales_country_week_ob(data_source, year, week, result)
